using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Text.RegularExpressions;
using Microsoft.Win32;

namespace eBirdAttend
{
    public partial class Employee : UserControl
    {
        public string strCon = Properties.Settings.Default["ConString"].ToString();
        SqlConnection sqlCon = new SqlConnection();
        SqlCommand sqlCmd = new SqlCommand();
        String strBtnCaption = "";
        //string _CardIDChd;
        
        public Employee()
        {
            InitializeComponent();
            sqlCon.ConnectionString = strCon;
            sqlCmd.Connection = sqlCon;
            //_CardIDChd = "";
        }

        private void Employee_Load(object sender, EventArgs e)
        {
            try
            {
                
                //employDetailsTableAdapter1.Fill(employeeDataset1.EmployDetails);
                //employDetailsTableAdapter1.Fill(dataSet1.EmployeeView);
                employeeViewTableAdapter.Connection.ConnectionString = Properties.Settings.Default["ConnectionString"].ToString();
                //MessageBox.Show(employeeViewTableAdapter.Connection.ConnectionString.ToString());
                //employeeViewTableAdapter.Connection = sqlCon;
                employeeViewDataset.EmployeeView.Columns[1].AllowDBNull = true;
                employeeViewTableAdapter.Fill(employeeViewDataset.EmployeeView);
                
                ArrayList EmpStatus = new ArrayList();
                EmpStatus.Add(new AddValue("[Select]", 0));
                EmpStatus.Add(new AddValue("Active", 1));
                EmpStatus.Add(new AddValue("Resigned", 2));
                EmpStatus.Add(new AddValue("Suspended/Long Leave", 3));

                this.cmbEmpStatus.DataSource = EmpStatus;
                this.cmbEmpStatus.DisplayMember = "Display";
                this.cmbEmpStatus.ValueMember = "Value";

                cmbEmpStatus.SelectedIndex = 0;

                departmentDataset1.Clear();
                departmentTableAdapter1.Fill(departmentDataset1.Department);

                DataTable table1 = new DataTable("Table1");

                table1 = departmentDataset1.Tables[0].Clone();

                //Add some rows.
                DataRow row;
                row = table1.NewRow();
                row["depid"] = 0;
                row["depname"] = "[Select]";
                row["depcmpid"] = "1";
                row["dephod"] = "Select";
                table1.Rows.Add(row);

                // Accept changes.
                table1.AcceptChanges();

                departmentDataset1.Tables[0].Constraints.Clear();
                DataTable table2 = departmentDataset1.Tables[0];

                table1.Merge(table2);

                cmbDepartmentID.DataSource = table1;
                cmbDepartmentID.DisplayMember = "depname";
                cmbDepartmentID.ValueMember = "depid";


                designationTableAdapter1.Fill(designationDataset1.Designation);
                table1 = new DataTable("Table1");

                table1 = designationDataset1.Tables[0].Clone();

                // Add some rows.

                row = table1.NewRow();
                row["desid"] = 0;
                row["desdesig"] = "[Select]";

                table1.Rows.Add(row);

                // Accept changes.
                table1.AcceptChanges();

                designationDataset1.Tables[0].Constraints.Clear();
                table2 = designationDataset1.Tables[0];
                table1.Merge(table2);

                cmbDesigId.DataSource = table1;
                cmbDesigId.DisplayMember = "desdesig";
                cmbDesigId.ValueMember = "desid";

                companyTableAdapter1.Fill(companyDataset1.Company);

                branchTableAdapter1.Fill(branchDataset1.Branch);
                table1 = new DataTable("Table1");

                table1 = branchDataset1.Tables[0].Clone();

                // Add some rows.
                row = table1.NewRow();
                row["brbranchid"] = 0;
                row["brcmpid"] = 0;
                row["brname"] = "[Select]";

                table1.Rows.Add(row);

                // Accept changes.
                table1.AcceptChanges();

                branchDataset1.Tables[0].Constraints.Clear();
                table2 = branchDataset1.Tables[0];
                table1.Merge(table2);

                cmbBranchID.DataSource = table1;
                cmbBranchID.DisplayMember = "brname";
                cmbBranchID.ValueMember = "brbranchid";

                
                string _strc = Properties.Settings.Default["ConnectionString"].ToString();
                shiftDetailsTableAdapter.Connection.ConnectionString = _strc;
                shiftDetailsTableAdapter.Fill(shiftDetailsDataset1.ShiftDetails);
                table1 = new DataTable("Table1");

                table1 = shiftDetailsDataset1.Tables[0].Clone();

                // Add some rows.
                row = table1.NewRow();
                row["shid"] = 0;
                row["shdesc"] = "select";
                row["shname"] = "[Select]";
                row["ShMinTimeFrHalfDay"] = "1";
                row["ShMinTimeFrFullDay"] = "1";
                

                table1.Rows.Add(row);

                // Accept changes.
                table1.AcceptChanges();

                shiftDetailsDataset1.Tables[0].Constraints.Clear();
                table2 = shiftDetailsDataset1.Tables[0];
                table1.Merge(table2);

                cmbShift.DataSource = table1;
                cmbShift.DisplayMember = "shname";
                cmbShift.ValueMember = "shid";  

                empView.Columns[0].Visible = false;

                tabControl1.TabIndex = 0;
            }
            catch (Exception ex)
            {
            }
  
        }



        // Function name   : btnAdd_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
           
            ClearControls();
            groupBox1.Visible = true;
            strBtnCaption = "Add";
            groupBox1.Text = "ADD AN EMPLOYEE";

            ////set the format to custom
            //this.DateDob.Format =
            //System.Windows.Forms.DateTimePickerFormat.Custom;
            //// set custom format to empty
            //this.DateDob.CustomFormat = " " ;


            //if (GetCardId() == 0)
            //{
                //if (txtCardId.Text == "0")
                //{
                //    txtCardId.Text = "1";
                //}

                try
                {
                    sqlCmd = new SqlCommand();
                    sqlCmd.Parameters.Clear();
                    sqlCmd.CommandText = "select max(empid) from employdetails";
                    sqlCmd.Connection = sqlCon;
                    sqlCon.Open();
                    SqlDataReader sqlReader;
                    sqlReader = sqlCmd.ExecuteReader();
                    if (sqlReader.Read())
                    {
                        if (sqlReader[0].ToString() == "")
                        {
                        }
                        else
                        {
                            txtEmpId.Text = Convert.ToString(Convert.ToInt32(sqlReader[0].ToString()) + 1);
                        }
                    }
                    else
                    {
                        txtEmpId.Text = "1";
                    }

                    if (txtEmpId.Text == "")
                    {
                        txtEmpId.Text = "1";
                    }
                    //txtCardId.Focus();
                    sqlReader.Close();
                    sqlCon.Close();


                    sqlCmd = new SqlCommand();
                    sqlCmd.Parameters.Clear();
                    sqlCmd.CommandText = "select cmpname from company";
                    sqlCmd.Connection = sqlCon;
                    sqlCon.Open();
                    sqlReader = sqlCmd.ExecuteReader();
                    if (sqlReader.Read())
                    {
                        if (sqlReader[0].ToString() == "")
                        {
                        }
                        else
                        {
                            txtCompanyId.Text = Convert.ToString(sqlReader[0].ToString());
                        }
                    }

                }
                catch (Exception e1)
                {

                }
                finally
                {
                    sqlCon.Close();

                }
            //}
        }


        // Function name   : btnSave_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckComboBoxes())
            {
                //check for the duplicate value of the employeecode
                if (CheckEmployeeCode())
                {

                    if (txtEmail.Text != "")
                    {
                        if (CheckValidMailId())
                        {
                            lblValidEmail.Visible = false;
                        }
                        else
                        {
                            lblValidEmail.Visible = true;
                            return;
                        }
                    }
                        lblValidEmail.Visible = false;
                        //_CardIDChd = "";
                        //insert the new employee details
                        if (strBtnCaption == "Add")
                        {
                            //if (GetCardId() != 0) return;
                            while (empView.Rows.Count > 1)
                                empView.Rows.RemoveAt(0);
                            sqlCmd = new SqlCommand();
                            sqlCmd.Parameters.Clear();
                            sqlCmd.CommandText = "insert into EmployDetails values('" + txtEmpCode.Text + "','" + txtFirstName.Text
                                                 + "','" + txtLastName.Text + "','" + "1" + "','"
                                                 + cmbBranchID.SelectedValue + "','"
                                                 + cmbDepartmentID.SelectedValue + "','"
                                                 + cmbDesigId.SelectedValue + "','"
                                                 + cmbShift.SelectedValue + "','" + txtAddress.Text + "','"
                                                 + txtPhone.Text + "','" + txtMobile.Text + "','" + DateDob.Value.ToString("MM/dd/yyyy") + "','"
                                                 + DateJoining.Value.ToString("MM/dd/yyyy") + "','" + DateResig.Value.ToString("MM/dd/yyyy") + "','" + cmbEmpStatus.SelectedValue.ToString()
                                                 + "','" + txtEmail.Text + "')";
                            sqlCmd.Connection = sqlCon;
                            sqlCon.Open();
                            sqlCmd.ExecuteNonQuery();
                            sqlCon.Close();
                            groupBox1.Visible = false;
                            employeeViewTableAdapter.Connection.ConnectionString = Properties.Settings.Default["ConnectionString"].ToString();
                            //MessageBox.Show(employeeViewTableAdapter.Connection.ConnectionString.ToString());
                            // employeeViewTableAdapter.Connection = sqlCon;
                            employeeViewTableAdapter.Fill(employeeViewDataset.EmployeeView);
                            MessageBox.Show("Successfuly Inserted");

                            //if (DialogResult.Yes == MessageBox.Show("Do you want to enroll this employee in device", "CONFIRM", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                            //{
                            //    SelectPort _SelectPort = new SelectPort();
                            //    _SelectPort.EmployID = empView.SelectedRows[0].Cells[0].Value.ToString();
                            //    _SelectPort.ShowDialog();
                       
                            //    //SelectPort EnrollDlg = new SelectPort();
                            //    ////EnrollDlg._strCardID = txtCardId.Text;
                            //    //EnrollDlg.EmployID = empView.SelectedRows[0].Cells[0].Value.ToString();
                            //    //EnrollDlg.ShowDialog();
                            //    ////string strPort = _SelectPort.strPort;

                            //}
                            ClearControls();

                        }
                        else
                        {
                            //bool CardIDChanged = false;
                            //update the employee details
                            //if (_CardIDChd != txtCardId.Text)
                            //{
                            //    CardIDChanged = true;
                            //    if (GetCardId() != 0) return;
                            //}
                            while (empView.Rows.Count > 1)
                                empView.Rows.RemoveAt(0);
                            sqlCmd = new SqlCommand();
                            sqlCmd.Parameters.Clear();
                            sqlCmd.CommandText = "update  EmployDetails set EmpFirstName='" + txtFirstName.Text
                                                + "',Empcode='" + txtEmpCode.Text
                                                + "',EmpLastName='" + txtLastName.Text + "',EmpCmpId='" + "1"
                                                + "' ,EmpDesigId='" + cmbDesigId.SelectedValue.ToString()
                                                + "' ,EmpShiftid='" + cmbShift.SelectedValue.ToString()
                                                + "',EmpDeptId='"
                                                + cmbDepartmentID.SelectedValue.ToString()
                                                + "' ,EmpBrId='" + cmbBranchID.SelectedValue.ToString() + "',EmpAddress='"
                                                + txtAddress.Text + "',EmpPhone='" + txtPhone.Text + "',EmpMobile='"
                                                + txtMobile.Text + "',EmpDOB='" + DateDob.Value.ToString("MM/dd/yyyy") + "',EmpJoinDate='"
                                                + DateJoining.Value.ToString("MM/dd/yyyy") + "',EmpResigDate='" + DateResig.Value.ToString("MM/dd/yyyy") + "' ,EmpStatus='"
                                                + cmbEmpStatus.SelectedValue.ToString() + "' ,EmpEmail='" + txtEmail.Text + "' where EmpId='"
                                                + txtEmpId.Text + "'";
                            sqlCmd.Connection = sqlCon;
                            sqlCon.Open();
                            sqlCmd.ExecuteNonQuery();
                            sqlCon.Close();
                            if (cmbEmpStatus.SelectedValue.ToString().Trim() == "2")
                            {
                                sqlCmd = new SqlCommand();
                                sqlCmd.Parameters.Clear();

                                sqlCmd.CommandText = "delete from DevEmpDetails where EmpID='" + txtEmpId.Text + "'";
                                sqlCmd.Connection = sqlCon;
                                sqlCon.Open();
                                sqlCmd.ExecuteNonQuery();
                                sqlCon.Close();
                            }
                            employeeViewTableAdapter.Connection.ConnectionString = Properties.Settings.Default["ConnectionString"].ToString();
                            // MessageBox.Show(employeeViewTableAdapter.Connection.ConnectionString.ToString());
                            // employeeViewTableAdapter.Connection = sqlCon;
                            employeeViewTableAdapter.Fill(employeeViewDataset.EmployeeView);
                            MessageBox.Show("Successfuly Updated");
                            //if (DialogResult.Yes == MessageBox.Show("Do you want to enroll this employee in device", "CONFIRM", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                            //{
                            //    SelectPort _SelectPort = new SelectPort();
                            //    _SelectPort.EmployID = empView.SelectedRows[0].Cells[0].Value.ToString();
                            //    _SelectPort.ShowDialog();

                            //    //SelectPort EnrollDlg = new SelectPort();
                            //    ////EnrollDlg._strCardID = txtCardId.Text;
                            //    //EnrollDlg.EmployID = empView.SelectedRows[0].Cells[0].Value.ToString();
                            //    //EnrollDlg.ShowDialog();
                            //    ////string strPort = _SelectPort.strPort;

                            //}                     
                        }
                       
                       ClearControls();
                    
                }
            }
                  
        }

        public bool CheckValidMailId()
        {
            string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" +
               @"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" +
               @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
            System.Text.RegularExpressions.Match match =
                Regex.Match(txtEmail.Text.Trim(), pattern, RegexOptions.IgnoreCase);

            if (match.Success)
                return true;
            else
                return false;
        }


        // Function name   : btnEdit_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnEdit_Click(object sender, EventArgs e)
        {
          
            if (empView.SelectedRows.Count == 0)
            { MessageBox.Show("Please select a row for edit"); return; }

            if ((empView.RowCount - 1) == empView.SelectedRows[0].Index)
            {
                MessageBox.Show("No content to edit");
                groupBox1.Visible = false;             
                ClearControls();
                return;
            }
            ClearControls();
            groupBox1.Visible = true;
            strBtnCaption = "Edit";
            groupBox1.Text = "EDIT AN EMPLOYEE";
            
        
            txtEmpCode.Text = empView.SelectedRows[0].Cells[1].Value.ToString();
            //txtCardId.Text = empView.SelectedRows[0].Cells[1].Value.ToString();
            //_CardIDChd = txtCardId.Text;
            txtEmpId.Text = empView.SelectedRows[0].Cells[0].Value.ToString();
            txtFirstName.Text = empView.SelectedRows[0].Cells[2].Value.ToString();
            txtLastName.Text = empView.SelectedRows[0].Cells[3].Value.ToString();
            txtCompanyId.Text = empView.SelectedRows[0].Cells[4].Value.ToString();
            cmbBranchID.ResetText();
            cmbBranchID.Text =empView.SelectedRows[0].Cells[5].Value.ToString();
            cmbDepartmentID.ResetText();
            cmbDepartmentID.Text = empView.SelectedRows[0].Cells[6].Value.ToString();         
            cmbDesigId.ResetText();
            cmbDesigId.Text = empView.SelectedRows[0].Cells[7].Value.ToString();
            cmbShift.ResetText();
            cmbShift.Text = empView.SelectedRows[0].Cells[8].Value.ToString();
     
            txtAddress.Text = empView.SelectedRows[0].Cells[9].Value.ToString();
            txtPhone.Text = empView.SelectedRows[0].Cells[10].Value.ToString();
            txtMobile.Text = empView.SelectedRows[0].Cells[11].Value.ToString();
            txtEmail.Text = empView.SelectedRows[0].Cells[12].Value.ToString();
            if(empView.SelectedRows[0].Cells[13].Value.ToString() != "")
            DateDob.Value = Convert.ToDateTime(empView.SelectedRows[0].Cells[13].Value.ToString());
            if(empView.SelectedRows[0].Cells[14].Value.ToString() != "")
            DateJoining.Value = Convert.ToDateTime(empView.SelectedRows[0].Cells[14].Value.ToString());
            if (empView.SelectedRows[0].Cells[15].Value.ToString() != "")
            DateResig.Value = Convert.ToDateTime(empView.SelectedRows[0].Cells[15].Value.ToString());
            cmbEmpStatus.Text = empView.SelectedRows[0].Cells[16].Value.ToString();
                   
        }


        // Function name   : btnDelete_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (empView.SelectedRows.Count == 0)
            { MessageBox.Show("Please select a row to delete"); return; }
            if (DialogResult.Yes == MessageBox.Show("Are you sure you want to delete this employee in device", "CONFIRM", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
            {
                string strDesId = empView.SelectedRows[0].Cells[0].Value.ToString();
                while (empView.Rows.Count > 1)
                    empView.Rows.RemoveAt(0);
                sqlCmd.CommandText = "delete from  EmployDetails where EmpId ='" + strDesId + "'";
                sqlCmd.Connection = sqlCon;
                sqlCon.Open();
                sqlCmd.ExecuteNonQuery();
                sqlCon.Close();

                sqlCmd.CommandText = "delete from DevEmpDetails where EmpID='" + strDesId + "'";
                sqlCmd.Connection = sqlCon;
                sqlCon.Open();
                sqlCmd.ExecuteNonQuery();
                sqlCon.Close();
                employeeViewTableAdapter.Fill(employeeViewDataset.EmployeeView);

                MessageBox.Show("Successfuly Deleted");
            }
            else { return; }
            
        }


        // Function name   : btnCancel_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/18/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
            btnEdit.Visible = true;
            ClearControls();
        }


        // Function name   : ClearControls
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/19/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        
        public void ClearControls()
        {
            txtEmpId.Text = "";
            //txtCardId.Text = "";
            txtEmpCode.Text = "";
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtAddress.Text = "";
            txtEmail.Text = "";
            txtMobile.Text = "";
            txtPhone.Text = "";
            cmbEmpStatus.SelectedIndex = 0;
            cmbBranchID.SelectedIndex = 0;
            cmbDepartmentID.SelectedIndex = 0;
            cmbDesigId.SelectedIndex = 0;
            cmbShift.SelectedIndex = 0;
            DateDob.Value = DateTime.Now.Date;
            DateJoining.Value = DateTime.Now.Date;
            DateResig.Value = DateTime.Now.Date;
            groupBox1.Visible = false;
            tabControl1.SelectedIndex = 0;
           
        }


        // Function name   : btnClose_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/22/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void txtEmpCode_MouseClick(object sender, MouseEventArgs e)
        {
            if (txtFirstName.Text != "")
            {
                if (txtFirstName.Text.Length > 3)
                {
                    txtEmpCode.Text = txtFirstName.Text.Substring(0, 3) + txtEmpId.Text;
                }
                else
                {
                    txtEmpCode.Text = txtFirstName.Text+ txtEmpId.Text;
                }
            }
        }     
     
        private void txtEmpCode_Enter(object sender, EventArgs e)
        {
            try
            {
                if (txtFirstName.Text != "")
                {
                    if (txtFirstName.Text.Length > 3)
                    {
                        txtEmpCode.Text = txtFirstName.Text.Substring(0, 3) + txtEmpId.Text;
                    }
                    else
                    {
                        txtEmpCode.Text = txtFirstName.Text + txtEmpId.Text;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        //private int GetCardId()
        //{
        //    try
        //    {
        //        sqlCmd = new SqlCommand();
        //        sqlCmd.Parameters.Clear();
        //        sqlCmd.CommandText = "GetCardId";
        //        sqlCmd.CommandType = CommandType.StoredProcedure;

        //        sqlCmd.Parameters.Clear();
        //        SqlParameter sqlParam = new SqlParameter("@AvailableCardId", SqlDbType.Int);
        //        sqlParam.Direction = ParameterDirection.Output;
        //        sqlCmd.Parameters.Add(sqlParam);

        //        sqlParam = new SqlParameter("@Status", SqlDbType.Int);
        //        sqlParam.Direction = ParameterDirection.Output;
        //        sqlCmd.Parameters.Add(sqlParam);

        //        sqlParam = new SqlParameter("@cardid_in", SqlDbType.VarChar);
        //        sqlParam.Direction = ParameterDirection.Input;

        //        if (txtCardId.Text != "")
        //        {
        //            sqlParam.Value = txtCardId.Text;
        //        }
        //        else
        //        {
        //            sqlParam.Value = "0";
        //        }

        //        sqlCmd.Parameters.Add(sqlParam);

        //        sqlCon.Open();

        //        sqlCmd.Connection = sqlCon;

        //        sqlCmd.ExecuteNonQuery();


        //        int AvailableCardId = Int32.Parse(sqlCmd.Parameters["@AvailableCardId"].Value.ToString());

        //        int Status = Int32.Parse(sqlCmd.Parameters["@Status"].Value.ToString());


        //        sqlCon.Close();

        //        if (Status == 1)
        //        {
        //            MessageBox.Show("Maximum of 200 Employees Only");
        //            return 1;
        //        }
        //        else if (Status == 2)
        //        {
        //            MessageBox.Show("Card id already exists in the employees table");
        //            txtCardId.Focus();
        //            return 2;
        //        }
        //        else
        //        {
        //            txtCardId.Text = AvailableCardId.ToString();
        //            if (txtCardId.Text == "0")
        //            {
        //                txtCardId.Text = "1";
        //            }
        //            return 0;
        //        }



        //        return 0;

        //    }
        //    catch (Exception exp)
        //    {
        //        MessageBox.Show(exp.Message.ToString());
        //        return 3;
        //    }

        //}

       

        // Function name   : CheckEmployeeCode
        // Description     : 
        // Return type     : bool 
        // Author          : Priya Purushothaman
        // Date            : 10/29/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 

        private bool CheckEmployeeCode()
        {
            sqlCmd.CommandText = "Select empcode from employdetails where empcode ='" + txtEmpCode.Text + "'";
            SqlDataReader sqlReader;
            try
            {
                sqlCon.Open();
                sqlReader = sqlCmd.ExecuteReader();
                if (sqlReader.Read())
                {
                    if (sqlReader[0].ToString() != null)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return true;
                }
            }
            catch
            {
            }
            finally
            {
                sqlCon.Close();
            }
            return true;
           
        }


        // Function name   : CheckComboBoxes
        // Description     : 
        // Return type     : bool 
        // Author          : Priya Purushothaman
        // Date            : 10/29/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        
        private bool CheckComboBoxes()
        {
            if (txtFirstName.Text == "")
            {
                MessageBox.Show("Enter the first name");
                txtFirstName.Focus();
                return false;
            }
            else if(txtLastName.Text == "")
            {
                MessageBox.Show("Enter the last name");
                txtLastName.Focus();
                return false;
            }
            else if (txtEmpCode.Text == "")
            {
                MessageBox.Show("Enter the employee code");
                txtEmpCode.Focus();
                return false;
            }
            else if (cmbDepartmentID.SelectedIndex == 0)
            {
                MessageBox.Show("Select the department");
                cmbDepartmentID.Focus();
                return false;
            }
            else if (cmbDesigId.SelectedIndex == 0)
            {
                MessageBox.Show("Select the designation");
                cmbDesigId.Focus();
                return false;
            }
            else if (cmbBranchID.SelectedIndex == 0)
            {
                MessageBox.Show("Select the branch");
                cmbBranchID.Focus();
                return false;
            }
            else if (cmbEmpStatus.SelectedIndex == 0)
            {
                MessageBox.Show("Select the status");
                cmbEmpStatus.Focus();
                return false;
            }
            else
            {
                return true;
            }


        }

        private void txtFirstName_TabIndexChanged(object sender, EventArgs e)
        {
            if (txtFirstName.Text.Trim() != "")
            {
                if (txtFirstName.Text.Length >= 3)
                {
                    txtEmpCode.Text = txtFirstName.Text.Substring(0, 3) + txtEmpId.Text;
                }
                else
                {
                    txtEmpCode.Text = txtFirstName.Text + txtEmpId.Text;
                }
            }
        }

        private void dateTimePicker3_ValueChanged(object sender, EventArgs e)
        {

        }

        private void btnEnroll_Click(object sender, EventArgs e)
        {
            if (empView.SelectedRows.Count == 0)
            { MessageBox.Show("Please select a row"); return; }

            if ((empView.RowCount - 1) == empView.SelectedRows[0].Index)
            {
                MessageBox.Show("No contents");
                groupBox1.Visible = false;
                ClearControls();
                return;
            }

            SelectPort _SelectPort = new SelectPort();
            //String empcode = empView.SelectedRows[0].Cells[1].Value.ToString();
            _SelectPort.EmployID = empView.SelectedRows[0].Cells[0].Value.ToString();
            //_SelectPort._strCardID = empcode;
            _SelectPort.Location = new Point(20, 0);
            _SelectPort.ShowDialog();
                       


        }


        private void ChangeTabColor(DrawItemEventArgs e)
        {
            Font TabFont;
            Brush BackBrush = new SolidBrush(Color.DarkGray); //Set background color
            Brush ForeBrush = new SolidBrush(Color.Black);//Set foreground color
            if (e.Index == this.tabControl1.SelectedIndex)
            {
                TabFont = new Font(e.Font, FontStyle.Italic|FontStyle.Bold);
            }
            else
            {
                TabFont = e.Font;
            }
            string TabName = this.tabControl1.TabPages[e.Index].Text;
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            e.Graphics.FillRectangle(BackBrush, e.Bounds);
            Rectangle r = e.Bounds;
            r = new Rectangle(r.X, r.Y + 3, r.Width, r.Height - 3);
            e.Graphics.DrawString(TabName, TabFont, ForeBrush, r, sf);
            //Dispose objects
            sf.Dispose();
            if (e.Index == this.tabControl1.SelectedIndex)
            {
                TabFont.Dispose();
                BackBrush.Dispose();
            }
            else
            {
                BackBrush.Dispose();
                ForeBrush.Dispose();
            }
        }


        private void DateDob_ValueChanged(object sender, EventArgs e)
        {
//            this.DateDob.Format =
//System.Windows.Forms.DateTimePickerFormat.Long; //or short.

        }

        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            ChangeTabColor(e);
        }

        private void txtEmpCode_TextChanged(object sender, EventArgs e)
        {

        }
       

       

      
        //private void txtCardId_TextChanged(object sender, EventArgs e)
        //{
           
        //}

    }
}
